/*
 * @Author: Snow
 * @Date: 2021-10-20 20:09:27
 * @Description: 
 */
import { WebGLUtil } from "../../utils/WebGLUtil";
import { Render } from "../Render";
import { Node } from "./Node";
export class MeshRenderer {
    private mesh: any;

    private meshProgramInfo:any;
    constructor(mesh,meshProgramInfo?:any) {
        this.mesh = mesh;
        this.meshProgramInfo = meshProgramInfo;
    }
    public render(node: Node, projection: number[], view: number[], sharedUniforms: any) {
        const { mesh } = this;
        const gl = Render.GL;
        gl.useProgram(this.meshProgramInfo.program);
        for (const primitive of mesh.primitives) {
            WebGLUtil.setBuffersAndAttributes(gl, this.meshProgramInfo, primitive.bufferInfo);
            WebGLUtil.setUniforms(this.meshProgramInfo, {
                u_projection: projection,
                u_view: view,
                u_world: node.worldMatrix,
            });
            WebGLUtil.setUniforms(this.meshProgramInfo, primitive.material.uniforms);
            WebGLUtil.setUniforms(this.meshProgramInfo, sharedUniforms);
            WebGLUtil.drawBufferInfo(gl, primitive.bufferInfo);
        }
    }
}